package com.real8.romemp.client.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.real8.romemp.client.ClientFactory;
import com.real8.romemp.client.GameConstants;
import com.real8.romemp.client.place.HireAuxiliaPlace;
import com.real8.romemp.client.place.LabourAllocationPlace;
import com.real8.romemp.client.place.LabourAllocationPlace.EMenuSelection;
import com.real8.romemp.client.ui.LabourAllocationView;
import com.real8.romemp.shared.GameData;

public class LabourAllocationActivity extends AbstractActivity implements
		LabourAllocationView.Presenter {
	private final ClientFactory clientFactory;
	private EMenuSelection menuOption;
	private LabourAllocationView view;

	public LabourAllocationActivity(LabourAllocationPlace place,
			ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		this.menuOption = place.getMenuOption();
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getLabourAllocationView();
		if (view != null) {
			view.setPresenter(this);

			// prepare view
			view.hideAll();

			// check free labour
			GameData gameData = clientFactory.getGameData();
			int freeLabour = gameData.getUL_freeLabour();
			if (freeLabour > 0) {
				// show yearly report
				view
						.showYearlyReport(-gameData.getTN_year(), gameData
								.getPO_population(), gameData
								.getLA_labourArmy(), gameData
								.getMH_auxiliaHired(), gameData
								.getWS_warShips(), gameData.getLN_labourNavy(),
								gameData.getMS_merchantShips(), gameData
										.getLM_labourMerchants(), gameData
										.getLS_labourShipBuilders(), gameData
										.getLE_labourArtisans(), freeLabour,
								gameData.getTR_treasury(), gameData
										.getFS_foodReserve(), gameData
										.getFR_foodNeed());

				// show menu
				view.showMenu(gameData.getUL_freeLabour());

				// show question
				view.showAllocationQA(menuOption);
			} else {
				// show allocated
				view.showFullyAllocated();

				// delay and go to next step
				Timer timer = new Timer() {
					@Override
					public void run() {
						clientFactory.getPlaceController().goTo(
								new HireAuxiliaPlace());
					}
				};
				timer.schedule(GameConstants.DELAY_6010);
			}

			// set widget
			containerWidget.setWidget(view.asWidget());
		}
	}

	@Override
	public void onSelected(EMenuSelection selection) {
		goToMenu(selection);
	}

	private void goToMenu(EMenuSelection newMenuOption) {
		clientFactory.getPlaceController().goTo(
				new LabourAllocationPlace(newMenuOption));
	}

	@Override
	public void onAllocationAnswer(int allocation) {
		// check
		GameData gameData = clientFactory.getGameData();
		int freeLabour = gameData.getUL_freeLabour();
		if (allocation <= freeLabour) {
			switch (menuOption) {
			case ARMY:
				gameData.setUL_freeLabour(freeLabour - allocation);
				gameData.setLA_labourArmy(gameData.getLA_labourArmy()
						+ allocation);
				break;
			case NAVY:
				gameData.setUL_freeLabour(freeLabour - allocation);
				gameData.setLN_labourNavy(gameData.getLN_labourNavy()
						+ allocation);
				break;
			case MERCHANTS:
				gameData.setUL_freeLabour(freeLabour - allocation);
				gameData.setLM_labourMerchants(gameData.getLM_labourMerchants()
						+ allocation);
				break;
			case ARTISANS:
				gameData.setUL_freeLabour(freeLabour - allocation);
				gameData.setLE_labourArtisans(gameData.getLE_labourArtisans()
						+ allocation);
				break;
			case SHIP_BUILDERS:
				gameData.setUL_freeLabour(freeLabour - allocation);
				gameData.setLS_labourShipBuilders(gameData
						.getLS_labourShipBuilders()
						+ allocation);
				break;
			default:
				break;
			}

			// return to menu selection
			goToMenu(EMenuSelection.NONE);
		}
	}
}
